package com.yanqu.road.server.gameplayer.module.manor;

import com.yanqu.road.entity.config.manor.ManorGuestConfig;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.enums.eManorGuestType;
import com.yanqu.road.entity.log.LogManorGuestUnLock;
import com.yanqu.road.entity.log.LogManorSkinLvUp;
import com.yanqu.road.entity.manor.ManorGuestData;
import com.yanqu.road.entity.player.UserSkin;
import com.yanqu.road.logic.bussiness.player.UserManorBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.skin.SkinModule;
import com.yanqu.road.server.manger.config.SkinMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.manor.ManorMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/5/10 18:58
 */
public class ManorGuestModel {
    private Map<Integer, ManorGuestData> guestDataMap;
    private GamePlayer player;

    public ManorGuestModel(Map<Integer, ManorGuestData> guestDataMap, GamePlayer player) {
        this.guestDataMap = guestDataMap;
        this.player = player;
    }

    public Map<Integer, ManorGuestData> getGuestDataMap() {
        return guestDataMap;
    }

    /**
     * 住客解锁
     * @param id
     * @return
     */
    public int unLock(int id) {
        ManorGuestData guestData = guestDataMap.get(id);
        if (guestData != null) {
            return GameErrorCode.E_MANOR_OBJ_IS_UNLOCK;
        }
        ManorGuestConfig manorGuestConfig = ManorMgr.getManorGuestConfigMap().get(id);
        if (manorGuestConfig == null) {
            return GameErrorCode.E_MANOR_CONFIG_NO_FOUND;
        }
        if (manorGuestConfig.getUnlockType() == eManorGuestType.Patrons.getValue()) {
            if (!player.getModule(PatronsModule.class).getUserPatronsMap().containsKey(Integer.parseInt(manorGuestConfig.getUnlockPar()))) {
                return GameErrorCode.E_MANOR_UNLOCK_LIMIT;
            }
        } else if (manorGuestConfig.getUnlockType() == eManorGuestType.Beauty.getValue()) {
            if (player.getModule(BeautyModule.class).getUserBeauty(Integer.parseInt(manorGuestConfig.getUnlockPar())) == null) {
                return GameErrorCode.E_MANOR_UNLOCK_LIMIT;
            }
        } else {
            return GameErrorCode.E_MANOR_TYPE_ERROR;
        }
        //先算一下收获
        player.getModule(ManorModule.class).notifyGuestAdd(id);
        guestData = new ManorGuestData();
        guestData.setUserId(player.getUserId());
        guestData.setId(id);
        guestData.setSkinId(0);
        guestData.setSkinIdList(new ArrayList<>());
        guestData.setInsertOption();
        guestDataMap.put(id, guestData);
        player.getModule(ManorModule.class).addProsperousValue(manorGuestConfig.getUnlockReward(), "住客解锁");
        player.getModule(ManorModule.class).syncManorData();
        LogMgr.addLogManorGuestUnLock(new LogManorGuestUnLock(player.getUserId(), guestData.getId(), new Date()));
        return 0;
    }

    public int unLockSkin(int skinId) {
        //玩家有没有这个皮肤
        UserSkin userSkin = player.getModule(SkinModule.class).getUserSkin(skinId);
        if (userSkin == null) {
            return GameErrorCode.E_MANOR_UNLOCK_LIMIT;
        }
        //皮肤配置
        SkinInfo skinInfo = SkinMgr.getSkinInfo(skinId);
        if (skinInfo == null) {
            return GameErrorCode.E_SKIN_NOT_EXIST;
        }
        //根据皮肤找到对应的住客id
        int guestId = findGuestId(skinInfo.getType(), skinInfo.getBelongRole());
        if (guestId == 0) {
            return GameErrorCode.E_MANOR_CONFIG_NO_FOUND;
        }
        ManorGuestData guestData = guestDataMap.get(guestId);
        if (guestData == null) {
            return GameErrorCode.E_MANOR_OBJ_IS_LOCK;
        }
        if (guestData.getSkinIdList().contains(skinId)) {
            return GameErrorCode.E_MANOR_SKIN_IS_UNLOCK;
        }

//        player.getModule(ManorModule.class).notifyGuestSkinAdd(skinId);
        guestData.addSkinId(skinId);
        player.getModule(ManorModule.class).addProsperousValue(GameConfig.MANOR_PROSPEROUS_UNLOCK_SKIN, "住客皮肤解锁");
        player.getModule(ManorModule.class).syncManorData();
        LogMgr.addLogManorSkinLvUp(new LogManorSkinLvUp(player.getUserId(), skinId, guestId, "0", 0, new Date()));
        changeSkin(guestData.getId(), skinId);
        return 0;
    }

    /**
     * 通过皮肤找到对应的庄园住客的id
     * @param skinType
     * @param belongId
     * @return
     */
    private int findGuestId(int skinType, int belongId) {
        Map<Integer, ManorGuestConfig> manorGuestConfigMap = ManorMgr.getManorGuestConfigMap();
        for (ManorGuestConfig guestConfig : manorGuestConfigMap.values()) {
            if (guestConfig.getUnlockType() == skinType && Integer.parseInt(guestConfig.getUnlockPar()) == belongId) {
                return guestConfig.getId();
            }
        }
        return 0;
    }

    public void save() {
        for (ManorGuestData guestData : guestDataMap.values()) {
            if (guestData.isInsertOption()) {
                UserManorBusiness.add(guestData);
            } else if (guestData.isUpdateOption()) {
                UserManorBusiness.update(guestData);
            }
        }
    }

    public int changeSkin(int id, int skinId) {
        ManorGuestData guestData = guestDataMap.get(id);
        if (guestData == null) {
            return GameErrorCode.E_MANOR_OBJ_IS_LOCK;
        }
        if (skinId != 0 && !guestData.getSkinIdList().contains(skinId)) {
            return GameErrorCode.E_MANOR_SKIN_IS_LOCK;
        }
        guestData.setSkinId(skinId);
        player.getModule(ManorModule.class).syncManorData();
        return 0;
    }
}
